package logic

import (
	"context"
	"fmt"
	"hotel/model/mysql"
	"hotel/model/redis"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *hotelrpc.LonginRequest) (*hotelrpc.LonginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("sendsmslongin" + in.Tel)
	if get.Val() != in.Send {
		return nil, fmt.Errorf("短信验证码错误，请重新输入")
	}
	var findhotel mysql.Hotel
	mysql.DB.Find(&findhotel)
	if findhotel.ID == 0 {
		return nil, fmt.Errorf("该用户还没注册，请先去注册")
	}
	if findhotel.Name != in.Name {
		return nil, fmt.Errorf("没有该用户，请重新输入")
	}
	if findhotel.Tel != in.Tel {
		return nil, fmt.Errorf("用户手机号密码错误，请重新输入")
	}
	redis.Client.Del("sendsmslongin" + in.Tel)
	return &hotelrpc.LonginResponse{Id: int64(findhotel.ID)}, nil
}
